﻿using QQRobot.Services.Models.Response;
using System.Text;
using WebApiClient.Attributes;
using WebApiClient;

namespace QQRobot.Services
{
    public class Lottery
    {
        /// <summary>
        /// 大乐透
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GeBigLottoAsync()
        {
            try
            {
                string url = "https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=85&provinceId=0&pageSize=15&isVerify=1&pageNo=1";
                var api = HttpApi.Create<ILotteryAPI>();
                var bigLottoResult = await api.GeBigLottoAsync(url);
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendLine($"期号\t\t开奖日期\t\t\t开奖号码\t\t一等奖金额\t\t中奖注数");

                if (bigLottoResult != null && bigLottoResult.ErrorCode == "0"
                    && bigLottoResult.Value.List?.Any() == true)
                {
                    bigLottoResult.Value.List.ForEach(s =>
                    {
                        var one = s.PrizeLevelList.FirstOrDefault(x => x.AwardType == 0);//一等奖信息
                        if (one != null)
                        {
                            stringBuilder.AppendFormat($"{s.LotteryDrawNum}\t{s.LotteryDrawTime}\t{s.LotteryDrawResult}\t{one.StakeAmount}元\t{one.StakeCount}\n");
                        }
                    });
                    return stringBuilder.ToString();
                }
                return string.Empty;
            }
            catch (Exception e)
            {

                throw;
            }
        }


        //public const string UnionLottoLogo = "https://www.cwl.gov.cn/images/logo-top.png";

        /// <summary>
        /// 福利彩票
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetUnionLottoAsync()
        {
            try
            {
                var url = "https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo=1&pageSize=30&week=&systemType=PC";
                var api = HttpApi.Create<ILotteryAPI>();
                var unionLottoResult = await api.GetUnionLottoAsync(url);
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendLine($"期号\t\t开奖日期\t\t\t开奖号码\t\t一等奖金额\t\t中奖注数");

                if (unionLottoResult != null && unionLottoResult.State == 0
                    && unionLottoResult.Result?.Any() == true)
                {
                    unionLottoResult.Result.ForEach(s =>
                    {
                        var one = s.PrizeGrades.FirstOrDefault(x => x.Type == 1);//一等奖信息
                        if (one != null)
                        {
                            stringBuilder.AppendFormat($"{s.Code}\t{s.Date}\t{s.Red}+{s.Blue}\t{one.TypeMoney}元\t{one.TypeNum}\n");
                        }
                    });
                    return stringBuilder.ToString();
                }
                return string.Empty;
            }
            catch (Exception e)
            {

                throw;
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public interface ILotteryAPI : IHttpApi
    {
        #region 体育彩票
        /// <summary>
        /// 获取大乐透彩票开奖号
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [JsonReturn]
        ITask<BigLottoResponse> GeBigLottoAsync([Uri] string url);

        /// <summary>
        /// 获取大乐透彩票开奖号
        /// </summary>
        /// <returns></returns>
        [HttpGet("https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=85&provinceId=0&pageSize=10&isVerify=1&pageNo=1")]
        ITask<string> GeBigLottoAsyncV2();

        /// <summary>
        /// 获取七星彩彩票开奖号
        /// </summary>
        /// <returns></returns>
        [HttpGet("https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=04&provinceId=0&pageSize=15&isVerify=1&pageNo=1")]
        ITask<string> GetSevenStarColorAsync();
        #endregion


        #region 福利彩票
        /// <summary>
        /// 获取双色球彩票开奖号
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Header("ContentType", "text/json;charset=UTF-8")]
        [JsonReturn]
        ITask<UnionLottoResponse> GetUnionLottoAsync([Uri] string url);
        #endregion
    }

}